
#include "7SegInd.h"

static const uint8_t strobs[IND_STROB_COUNT] PROGMEM = {IND_STROB0, IND_STROB1, IND_STROB2, IND_STROB3};
static uint8_t strob_counter = 0;
uint8_t data[IND_STROB_COUNT] = {sym4, sym5, sym6, sym7};

uint8_t table_sym[] PROGMEM = {sym0, sym1, sym2, sym3, sym4, sym5, sym6, sym7, sym8, sym9};

void init_ind()
{
  INIT_IND_PORT();
  INIT_IND_STROB_PORT();
}

void task_ind()
{
  CLEAR_IND_STROBS();
  IND_PORT = data[strob_counter];
  IND_STROB_PORT |= pgm_read_byte(&strobs[strob_counter++]);
  if (strob_counter >= IND_STROB_COUNT) strob_counter = 0;
}

uint8_t get_symbol(uint8_t index)
{
  if (index < sizeof(table_sym))
  {
    return pgm_read_byte(&table_sym[index]);
  }
  else return 0;
}

void put_byte(uint8_t b)
{
  uint8_t index = 4;
  do {
    uint8_t d = b % 10;
    b /= 10;
    data[--index] = pgm_read_byte(&table_sym[d]);
  } while (b || index);
  while (index)
  {
    data[--index] = 0;
  }
}

void put_word(uint16_t b)
{
  uint8_t index = 4;
  do {
    uint8_t d = b % 10;
    b /= 10;
    data[--index] = pgm_read_byte(&table_sym[d]);
  } while (b || index);
  while (index)
  {
    data[--index] = 0;
  }
}
